#include "MatrixOperations.h"
#include <limits>

using namespace Eigen;
using namespace std;

MatrixXd MatrixOperations::createMatrixFromConsole() {
    cout << "=== 从控制台创建矩阵 ===" << endl;
    int rows = getIntegerInput("请输入矩阵的行数: ");
    int cols = getIntegerInput("请输入矩阵的列数: ");
    
    MatrixXd matrix(rows, cols);
    cout << "请输入 " << rows << "x" << cols << " 矩阵的元素 (按行输入):" << endl;
    
    for (int i = 0; i < rows; ++i) {
        cout << "第 " << i + 1 << " 行: ";
        for (int j = 0; j < cols; ++j) {
            cin >> matrix(i, j);
        }
    }
    
    return matrix;
}

MatrixXd MatrixOperations::createFixedSizeMatrix(int rows, int cols) {
    return MatrixXd(rows, cols);
}

MatrixXd MatrixOperations::createZeroMatrix(int rows, int cols) {
    return MatrixXd::Zero(rows, cols);
}

MatrixXd MatrixOperations::createIdentityMatrix(int size) {
    return MatrixXd::Identity(size, size);
}

MatrixXd MatrixOperations::createOnesMatrix(int rows, int cols) {
    return MatrixXd::Ones(rows, cols);
}

MatrixXd MatrixOperations::createRandomMatrix(int rows, int cols) {
    return MatrixXd::Random(rows, cols);
}

MatrixXd MatrixOperations::addMatrices(const MatrixXd& A, const MatrixXd& B) {
    if (A.rows() != B.rows() || A.cols() != B.cols()) {
        throw invalid_argument("矩阵尺寸不匹配，无法进行加法运算");
    }
    return A + B;
}

MatrixXd MatrixOperations::subtractMatrices(const MatrixXd& A, const MatrixXd& B) {
    if (A.rows() != B.rows() || A.cols() != B.cols()) {
        throw invalid_argument("矩阵尺寸不匹配，无法进行减法运算");
    }
    return A - B;
}

MatrixXd MatrixOperations::multiplyMatrixScalar(const MatrixXd& A, double scalar) {
    return scalar * A;
}

MatrixXd MatrixOperations::multiplyMatrices(const MatrixXd& A, const MatrixXd& B) {
    if (A.cols() != B.rows()) {
        throw invalid_argument("矩阵尺寸不匹配，无法进行乘法运算");
    }
    return A * B;
}

MatrixXd MatrixOperations::transposeMatrix(const MatrixXd& A) {
    return A.transpose();
}

void MatrixOperations::printMatrixProperties(const MatrixXd& A, const std::string& name) {
    cout << "=== " << name << " 属性 ===" << endl;
    cout << "尺寸: " << A.rows() << "x" << A.cols() << endl;
    cout << "总元素数: " << A.size() << endl;
    cout << "是否为空: " << (isMatrixEmpty(A) ? "是" : "否") << endl;
    cout << endl;
}

std::pair<int, int> MatrixOperations::getMatrixSize(const MatrixXd& A) {
    return {A.rows(), A.cols()};
}

bool MatrixOperations::isMatrixEmpty(const MatrixXd& A) {
    return A.size() == 0;
}

void MatrixOperations::printMatrix(const MatrixXd& A, const std::string& name) {
    cout << "=== " << name << " ===" << endl;
    cout << A << endl;
}

int MatrixOperations::getIntegerInput(const std::string& prompt) {
    int value;
    while (true) {
        cout << prompt;
        if (cin >> value && value > 0) {
            break;
        } else {
            cout << "输入无效，请输入一个正整数: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    return value;
}

double MatrixOperations::getDoubleInput(const std::string& prompt) {
    double value;
    while (true) {
        cout << prompt;
        if (cin >> value) {
            break;
        } else {
            cout << "输入无效，请输入一个有效的数字: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    return value;
}